import Mock from 'mockjs'
import '@/mock/extend'
import {parseUrlParams} from '@/utils/request'
const BASE_URL = process.env.VUE_APP_API_BASE_URL
// const currentTime = new Date().getTime()

const materialArr = Mock.mock({
    'list|100': [
        {
            'id|+1':1,
            'materialName': '@csentence(3, 5)',
            'materialType|1': [
                'imgs',
                'video'
            ],
            'purposeType': '@integer(1,3)',
            'subTime': '@datetime',
            'examineStutas': '@integer(0,2)'
        }
    ]
})

Mock.mock(RegExp(`${BASE_URL}/materials`+ '.*'), 'get', (url) => {
    const params = parseUrlParams(url.url)
    let result = []
    let {page, pageSize} = params
    page = eval(page) - 1 || 0
    pageSize = eval(pageSize) || 10
    delete params.page
    delete params.pageSize
    console.log(params)
    //过滤params属性
    for(let key in params){
        if(params[key] === '-1' || params[key] === ''){
            delete params[key]
        }
    }
    //根据params过滤materialArr数据
    if(Object.keys(params).length != 0){
        result = materialArr.list.filter(item => {
            for (let [key, value] of Object.entries(params)) {
                if (item[key] != value) {
                    return false
                }
            }
            return true
        })
        console.log('22',result)
        return {
            code: 200,
            message: 'success',
            data: {
                page: page + 1,
                pageSize:pageSize,
                list: result
            }
        }
    }else{
        return {
            code: 200,
            message: 'success',
            data: {
                page: page + 1,
                pageSize:pageSize,
                list: materialArr.list
            }
        }
    }

    // return materialArr
})
